package com.example.star.view.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.star.adapter.TabPagerAdapter;
import com.google.android.material.tabs.TabLayout;
import android.widget.Button;
import android.widget.EditText;
import com.example.star.R;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    private List<Fragment> fragments = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 加载布局文件
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        // 初始化 Toolbar
        Toolbar toolbar = view.findViewById(R.id.toolbar_home);
        // 如果需要设置标题等操作，可以在这里进行
        // toolbar.setTitle("首页");

        // 初始化搜索框和按钮
        EditText etSearch = view.findViewById(R.id.et_search);
        Button btnSearch = view.findViewById(R.id.btn_search);

        // 初始化 TabLayout 和 ViewPager
        TabLayout tabLayout = view.findViewById(R.id.tab_layout);
        ViewPager viewPager = view.findViewById(R.id.view_pager);

        // 创建适配器
        TabPagerAdapter adapter = new TabPagerAdapter(getChildFragmentManager());

        // 添加 7 个 Fragment 和对应的标题
        Tab1Fragment tab1Fragment = new Tab1Fragment();
        Tab2Fragment tab2Fragment = new Tab2Fragment();
        Tab3Fragment tab3Fragment = new Tab3Fragment();
        Tab4Fragment tab4Fragment = new Tab4Fragment();
        Tab5Fragment tab5Fragment = new Tab5Fragment();
        Tab6Fragment tab6Fragment = new Tab6Fragment();
        Tab7Fragment tab7Fragment = new Tab7Fragment();

        fragments.add(tab1Fragment);
        fragments.add(tab2Fragment);
        fragments.add(tab3Fragment);
        fragments.add(tab4Fragment);
        fragments.add(tab5Fragment);
        fragments.add(tab6Fragment);
        fragments.add(tab7Fragment);

        adapter.addFragment(tab1Fragment, "歌手");
        adapter.addFragment(tab2Fragment, "演员");
        adapter.addFragment(tab3Fragment, "短剧演员");
        adapter.addFragment(tab4Fragment, "体育健将");
        adapter.addFragment(tab5Fragment, "舞团");
        adapter.addFragment(tab6Fragment, "商界明星");
        adapter.addFragment(tab7Fragment, "“民”星");

        // 设置适配器
        viewPager.setAdapter(adapter);

        // 将 TabLayout 和 ViewPager 关联起来
        tabLayout.setupWithViewPager(viewPager);

        // 为按钮添加点击事件监听器
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String searchText = etSearch.getText().toString();
                for (Fragment fragment : fragments) {
                    if (fragment instanceof Tab1Fragment) {
                        ((Tab1Fragment) fragment).filter(searchText);
                    } else if (fragment instanceof Tab2Fragment) {
                        ((Tab2Fragment) fragment).filter(searchText);
                    } else if (fragment instanceof Tab3Fragment) {
                        ((Tab3Fragment) fragment).filter(searchText);
                    } else if (fragment instanceof Tab4Fragment) {
                        ((Tab4Fragment) fragment).filter(searchText);
                    } else if (fragment instanceof Tab5Fragment) {
                        ((Tab5Fragment) fragment).filter(searchText);
                    } else if (fragment instanceof Tab6Fragment) {
                        ((Tab6Fragment) fragment).filter(searchText);
                    } else if (fragment instanceof Tab7Fragment) {
                        ((Tab7Fragment) fragment).filter(searchText);
                    }
                }
            }
        });

        return view;
    }
}